import os, shutil
import cPickle as pickle


_save_dir = os.path.join('data', 'save', 'hero')
_game_path = os.path.join(_save_dir, 'game.save')
_level_path = os.path.join(_save_dir, 'level-%d.save')


def make_save_dir():
    try:
        os.makedirs(_save_dir)
    except OSError:
        pass

def save_game(game):
    make_save_dir()
    file = open(_game_path, 'wb')
    pickle.dump(game, file, pickle.HIGHEST_PROTOCOL)


def load_game():
    file = open(_game_path, 'rb')
    return pickle.load(file)


def remove_save_file():
    os.remove(_game_path)


def save_level(level):
    make_save_dir()
    file = open(_level_path % level.num, 'wb')
    pickle.dump(level, file, pickle.HIGHEST_PROTOCOL)


def load_level(level_num):
    file = open(_level_path % level_num, 'rb')
    return pickle.load(file)
